package com.dp.iot.admin.api.iotworkshop;

import com.dp.iot.core.service.CommonRestController;
import org.springframework.beans.factory.InitializingBean;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.math.*;
import java.util.List;
import java.util.Map;
import com.dp.iot.core.common.constant.PageConstant;
import com.dp.iot.core.common.exception.BusinessException;
import com.dp.iot.core.serialize.ResponseMsg;
import com.dp.iot.model.IotWorkShop;
import com.dp.iot.service.iotworkshop.service.IotWorkShopService;

import com.dp.iot.core.utils.ListUtil;
import com.dp.iot.core.utils.MapUtil;
import com.dp.iot.core.utils.StringUtil;


/**
* 代码为自动生成 Created by www.magicalcoder.com
* 如果你改变了此类 read 请将此行删除
* 欢迎加入官方QQ群:323237052
*/

@RequestMapping("/admin/iot_work_shop_rest/")
@RestController
public class AdminIotWorkShopRestController extends CommonRestController<IotWorkShop,String> implements InitializingBean
{

    @Resource
    private IotWorkShopService iotWorkShopService;

    @RequestMapping(value = "search")
    public ResponseMsg search(
        @RequestParam(required = false) String uniqueField,
        @RequestParam(required = false) String uniqueValue,
        @RequestParam(required = false,defaultValue = "20") Integer limit,
        @RequestParam(required = false) String keyword
    ){
        limit = Math.min(PageConstant.MAX_LIMIT,limit);
        List<IotWorkShop> list = null;
        Map<String,Object> query = new HashedMap();
        query.put("limit",limit);
        query.put("notSafeOrderBy","work_shop_id desc");
        if(uniqueValue!=null){//说明是来初始化的
            query.put(uniqueField,uniqueValue);
            list = iotWorkShopService.getModelList(query);
        }else {//正常搜索
            if(ListUtil.isBlank(list)){
                query.put("workShopCodeFirst",keyword);
                list = iotWorkShopService.getModelList(query);
                query.remove("workShopCodeFirst");
            }
            if(ListUtil.isBlank(list)){
                query.put("workShopNameFirst",keyword);
                list = iotWorkShopService.getModelList(query);
                query.remove("workShopNameFirst");
            }
            if(ListUtil.isBlank(list)){
                query.put("orgIdFirst",keyword);
                list = iotWorkShopService.getModelList(query);
                query.remove("orgIdFirst");
            }
            if(ListUtil.isBlank(list)){
                query.put("workShopTypeFirst",keyword);
                list = iotWorkShopService.getModelList(query);
                query.remove("workShopTypeFirst");
            }
            if(ListUtil.isBlank(list)){
                query.put("workShopStatusFirst",keyword);
                list = iotWorkShopService.getModelList(query);
                query.remove("workShopStatusFirst");
            }
            if(ListUtil.isBlank(list)){
                query.put("workShopCountryFirst",keyword);
                list = iotWorkShopService.getModelList(query);
                query.remove("workShopCountryFirst");
            }
            if(ListUtil.isBlank(list)){
                query.put("workShopProvinceFirst",keyword);
                list = iotWorkShopService.getModelList(query);
                query.remove("workShopProvinceFirst");
            }
            if(ListUtil.isBlank(list)){
                query.put("workShopCityFirst",keyword);
                list = iotWorkShopService.getModelList(query);
                query.remove("workShopCityFirst");
            }
            if(ListUtil.isBlank(list)){
                query.put("workShopLongitudeFirst",keyword);
                list = iotWorkShopService.getModelList(query);
                query.remove("workShopLongitudeFirst");
            }
            if(ListUtil.isBlank(list)){
                query.put("workShopDimensionFirst",keyword);
                list = iotWorkShopService.getModelList(query);
                query.remove("workShopDimensionFirst");
            }
            if(ListUtil.isBlank(list)){
                query.put("remarksFirst",keyword);
                list = iotWorkShopService.getModelList(query);
                query.remove("remarksFirst");
            }
            if(ListUtil.isBlank(list)){
                query.put("creationUserFirst",keyword);
                list = iotWorkShopService.getModelList(query);
                query.remove("creationUserFirst");
            }
            if(ListUtil.isBlank(list)){
                query.put("updateUserFirst",keyword);
                list = iotWorkShopService.getModelList(query);
                query.remove("updateUserFirst");
            }
            if(ListUtil.isBlank(list)){
                query.put("spare1First",keyword);
                list = iotWorkShopService.getModelList(query);
                query.remove("spare1First");
            }
            if(ListUtil.isBlank(list)){
                query.put("spare2First",keyword);
                list = iotWorkShopService.getModelList(query);
                query.remove("spare2First");
            }
            if(ListUtil.isBlank(list)){
                query.put("spare3First",keyword);
                list = iotWorkShopService.getModelList(query);
                query.remove("spare3First");
            }
        }
        return new ResponseMsg(list);
    }
    //分页查询
    @RequestMapping(value={"page"}, method={RequestMethod.GET})
    public ResponseMsg page(
        @RequestParam(required = false,value ="workShopIdFirst")                            String workShopIdFirst ,
        @RequestParam(required = false,value ="workShopCodeFirst")                            String workShopCodeFirst ,
        @RequestParam(required = false,value ="workShopNameFirst")                            String workShopNameFirst ,
        @RequestParam(required = false,value ="orgIdFirst")                            String orgIdFirst ,
        @RequestParam(required = false,value ="workShopTypeFirst")                            String workShopTypeFirst ,
        @RequestParam(required = false,value ="workShopStatusFirst")                            String workShopStatusFirst ,
        @RequestParam(required = false,value ="workShopCountryFirst")                            String workShopCountryFirst ,
        @RequestParam(required = false,value ="workShopProvinceFirst")                            String workShopProvinceFirst ,
        @RequestParam(required = false,value ="workShopCityFirst")                            String workShopCityFirst ,
        @RequestParam(required = false,value ="workShopLongitudeFirst")                            String workShopLongitudeFirst ,
        @RequestParam(required = false,value ="workShopDimensionFirst")                            String workShopDimensionFirst ,
        @RequestParam(required = false,value ="remarksFirst")                            String remarksFirst ,
        @RequestParam(required = false,value ="creationTimeFirst")                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date creationTimeFirst ,
        @RequestParam(required = false,value ="creationTimeSecond")                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date creationTimeSecond ,
        @RequestParam(required = false,value ="creationUserFirst")                            String creationUserFirst ,
        @RequestParam(required = false,value ="updateTimeFirst")                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date updateTimeFirst ,
        @RequestParam(required = false,value ="updateTimeSecond")                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date updateTimeSecond ,
        @RequestParam(required = false,value ="updateUserFirst")                            String updateUserFirst ,
        @RequestParam(required = false,value ="spare1First")                            String spare1First ,
        @RequestParam(required = false,value ="spare2First")                            String spare2First ,
        @RequestParam(required = false,value ="spare3First")                            String spare3First ,
        @RequestParam int page,@RequestParam int limit,@RequestParam(required = false) String safeOrderBy)
    {
        limit = Math.min(limit, PageConstant.MAX_LIMIT);
        int start = (page - 1) * limit;
        Map<String,Object> query = new HashedMap();
        query.put("workShopIdFirst",coverBlankToNull(workShopIdFirst));
        query.put("workShopCodeFirst",coverBlankToNull(workShopCodeFirst));
        query.put("workShopNameFirst",coverBlankToNull(workShopNameFirst));
        query.put("orgIdFirst",coverBlankToNull(orgIdFirst));
        query.put("workShopTypeFirst",coverBlankToNull(workShopTypeFirst));
        query.put("workShopStatusFirst",coverBlankToNull(workShopStatusFirst));
        query.put("workShopCountryFirst",coverBlankToNull(workShopCountryFirst));
        query.put("workShopProvinceFirst",coverBlankToNull(workShopProvinceFirst));
        query.put("workShopCityFirst",coverBlankToNull(workShopCityFirst));
        query.put("workShopLongitudeFirst",coverBlankToNull(workShopLongitudeFirst));
        query.put("workShopDimensionFirst",coverBlankToNull(workShopDimensionFirst));
        query.put("remarksFirst",coverBlankToNull(remarksFirst));
        query.put("creationTimeFirst",creationTimeFirst);
        query.put("creationTimeSecond",creationTimeSecond);
        query.put("creationUserFirst",coverBlankToNull(creationUserFirst));
        query.put("updateTimeFirst",updateTimeFirst);
        query.put("updateTimeSecond",updateTimeSecond);
        query.put("updateUserFirst",coverBlankToNull(updateUserFirst));
        query.put("spare1First",coverBlankToNull(spare1First));
        query.put("spare2First",coverBlankToNull(spare2First));
        query.put("spare3First",coverBlankToNull(spare3First));
        Integer count = iotWorkShopService.getModelListCount(query);
        query.put("start",start);
        query.put("limit",limit);
        if(StringUtil.isBlank(safeOrderBy)){
            query.put("notSafeOrderBy","work_shop_id desc");
        }else{
            query.put("safeOrderBy",safeOrderBy);
        }
        return new ResponseMsg(count,iotWorkShopService.getModelList(query));
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        super.commonService = iotWorkShopService;
        super.primaryKey = "workShopId";//硬编码此实体的主键名称
    }
}
